<?php
	/**
	 *	KLF Framework
	 *
	 *	Written by Kristian Oye <kristianoye@gmail.com> on Oct 22, 2011
	 *	Copyright (C) 2010-2011
	 */
	namespace KLF\System\Collections
	{
		use KLF\KCore;
		use KLF\System\KType;
		use KLF\System\KObject;
		use KLF\System\Errors\KArgumentException;
		
		/**
		 *  Class KCollection
		 *
		 *  @package KLF\System\Collections
		 *  @access public
		 *  @author Kristian Oye <kristianoye@gmail.com>
		 *  @since 0.1
		 *  
		 *  @method KCollection NewInstance
		 */
		abstract class KCollection extends KObject implements 
				\IteratorAggregate, \ArrayAccess, \Countable
		{
			/**
			 * Helper function to validate whether or not a key or value is
			 * of the expected type.
			 * 
			 * @param mixed $val
			 * @param mixed $expected
			 * @since 0.1
			 * @return boolean True if the type is expected, false if it is not.
			 */
			protected static function __validateValue($val, $context='key', $expected=null)
			{
				$result = false;
				
				if (is_null($expected))
				{
					$result = true;
				}
				else if (class_exists($expected, false) || interface_exists($expected, false))
				{
					$result = (is_object($val) && is_a($val, $expected));
				}
				else
				{
					$result = (KType::GetType($val) === $expected);
				}
				if (!$result)
				{
					throw new KArgumentException(
						KCore::FS("KDictionary: Key {context} must be {expected} not {got}",
							array(
								'context'	=> $context,
								'expected'	=> KType::GetTypeByClass($expected),
								'got'		=> KType::GetType($val))));
				}
				return $result;
			}
		}
	}
